from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver

from datetime import datetime

from selenium.webdriver.common.action_chains import ActionChains

from selenium.common.exceptions import NoSuchWindowException, TimeoutException, WebDriverException


class Base():
    #初始化浏览器
    def __init__(self, driver):
        self.driver = driver

    #打开首页地址
    def ope_url(self, url):
        self.driver.get(url)

    #封装元素定位方式
    def find_element(self, loc):
        try:
            #显示等待
            WebDriverWait(self.driver, 20, 0.5).until(lambda driver: driver.find_element(*loc).is_displayed())

            return self.driver.find_element(*loc)

        except Exception as e:
            data_time = datetime.now().strftime('%Y%m%d %H%M%S')
            self.driver.save_screenshot(f'err_{data_time}.png')

            print('未定位到元素', e)

    def frame(self, loc):
        self.driver.switch_to.frame(self.find_element(loc))

    def default_content(self):
        self.driver.switch_to.default_content()

    def send_keys(self, loc, info):
        self.find_element(loc).send_keys(info)

    def click(self, loc):
        self.find_element(loc).click()

    def ActionChains(self, lco):
        ele = self.find_element(lco)

        ActionChains(self.driver).move_to_element(ele).perform()

    def affirm_text(self, loc):
        return self.find_element(loc).text
